package webmarket.common;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 
 * STRING constants to put to the user interface
 * 
 */
public class UserInfoMessages {
	/**
	 * public static final String properties to reach messages easier (as
	 * constants)
	 */
	public static final String INVALID_RANGE = get("INVALID_RANGE");
	public static final String INVALID_INPUT_FORMAT = get("INVALID_INPUT_FORMAT");
	public static final String LOGIN_SUCCESS = get("LOGIN_SUCCESS");
	public static final String LOGOUT_SUCCESS = get("LOGOUT_SUCCESS");
	public static final String LOGIN_DENIED = get("LOGIN_DENIED");
	public static final String EDIT_SUCCESSFUL = get("EDIT_SUCCESSFUL");
	public static final String NO_UPLOADED_FILE = get("NO_UPLOADED_FILE");
	public static final String ERROR_READING_FILE = get("ERROR_READING_FILE");
	public static final String ERROR_WRITING_FILE = get("ERROR_WRITING_FILE");
	public static final String UPLOAD_SUCCESS = get("UPLOAD_SUCCESS");
	public static final String MODAL_LOADING = get("MODAL_LOADING");
	public static final String SUCCESSFUL_REGISTRATION = get("SUCCESSFUL_REGISTRATION");
	public static final String USERNAME_EXISTS = get("USERNAME_EXISTS");
	public static final String NEEDED_PARAMETER = get("NEEDED_PARAMETER");
	public static final String PASSWORD_LENGTH_ERROR = get("PASSWORD_LENGTH_ERROR");
	public static final String PICTURE_DELETE_SUCCESS = get("PICTURE_DELETE_SUCCESS");
	public static final String PICTURE_DELETE_ERROR = get("PICTURE_DELETE_ERROR");
	public static final String PICTURE_DELETE_WARNING = get("PICTURE_DELETE_WARNING");

	/**
	 * Store for the messages
	 */
	private static Map<String, String> messages;

	private static boolean wasInit = false;

	/**
	 * Lazyloading init
	 * Read userInfoMessages xml, and store messages in the hashmap
	 */
	public static void init() {
		if (!wasInit) {
			messages=new HashMap<String,String>();
			try {
				InputStream xmlStream = UserInfoMessages.class.getResourceAsStream("/userInfoMessages.xml");
				DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
				Document doc = dBuilder.parse(xmlStream);

				// optional, but recommended
				// read this -
				// http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
				doc.getDocumentElement().normalize();

				NodeList nList = doc.getElementsByTagName("message");

				for (int temp = 0; temp < nList.getLength(); temp++) {

					Node nNode = nList.item(temp);

					if (nNode.getNodeType() == Node.ELEMENT_NODE) {

						Element eElement = (Element) nNode;
						String name = eElement.getAttribute("name");
						String value = eElement.getAttribute("value");
						messages.put(name, value);

					}
				}
			} catch (Exception e) {
				// TODO: log init exception
			}
			wasInit=true;
		}
	}

	/**
	 * Gets the UserInfoMessage corresponding to given key
	 * 
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		init();
		if (messages.containsKey(key)) {
			return messages.get(key);
		} else {
			return "";
		}
	}

	/**
	 * @return the MODAL_LOADING_MSG
	 *         Needed to reach this constant from the JSP pages
	 */
	public String getMODAL_LOADING_MSG() {
		return MODAL_LOADING;
	}
}
